﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Finances;

@model FinancialIncomeBillModel

<script type="text/javascript">

	jQuery(document).ready(function () {

		"use strict";

		// Init Theme Core
		Core.init();

		// Init Demo JS
		Demo.init();

		if (("@Model.ReversedStatus").toLowerCase() == "true") {
			$("#inkpadBox").hide();
			$("#inkpadBox1").css('display', 'block');
		}

		//会计科目选择
		$(document).on('click', "#Accounting_AsyncSearchSelectPopup", function () {
            var billTypeId = $("#BillTypeEnumId").val();
            var actionUrl = "/Accounting/AsyncSearchSelectPopup?Multi=0&billTypeId=" + billTypeId;
			var accounts = $("#FinancialIncomeItems_Partial_Form").find("input[name='CollectionAmount']");
			if (accounts.length == 0) {
				$(this).showToastr("默认科目未指定！");
				return false;
			}
			$(this).accountingSearchSelectPopup($("#AccountingModalWindow"), actionUrl, null, "选择科目", $("#AccountingForm"), $("#CollectionAccount"), $("#CollectionAccountBox"));
		});


		//收款项目
		$("#FinancialIncomeItems").bootstrapTable({
                @Html.Raw(Model.Items.Count > 0 ? "url: '/FinancialIncome/AsyncFinancialIncomeItems?billId=" + Model.Id + "'," : "length: 9,")
			striped: true,
			cache: false,
			pagination: true,
			showPagination: false,
			sidePagination: "server",
			sortable: false,
			sortOrder: "asc",
			search: false,
			queryParams: function (params) {
				var temp =
					{
						cumtomerName: "",
						productName: "",
						pagenumber: (params.offset / params.limit)  //页码
					};
				return temp;
			},
			strictSearch: false,
			showColumns: false,
			showRefresh: false,
			clickToSelect: false,
			uniqueId: "id",
			showToggle: false,
			editable: true,
			cardView: false,
			detailView: false,
			showFooter: true,
			onLoadSuccess: function (data) {
				$(".fixed-table-pagination").hide();
			},
			columns: [
				{
					title: '<i class="fa fa-gear fs18"></i>',
					align: 'center',
					width: '50',
					formatter: function (value, row, index) {
						return m_pagerow + index + 1;
					},
					footerFormatter: function (value) {
						return '总计';
					}
				}, {
					field: 'AccountingOptionId',
					visible: false
				},
				{
					field: 'AccountingOptionName',
					title: '收入类别',
                    width: '200',
                    formatter: $(this).formatIsNull
				},
				{
					field: 'Amount',
					title: '金额',
					align: 'right',
					width: '100',
					formatter: $(this).formatFixed,
					footerFormatter: function (value) {
						var count = 0;
						var datas = $('#FinancialIncomeItems').bootstrapTable('getData');
						var len = datas.length,
							i, row;
						//console.log(len);
						if (len > 1) {
							for (i = len - 1; i >= 0; i--) {
								row = datas[i];
								count += parseFloat((typeof (row.Amount) == 'undefined' || row.Amount == "") ? '0' : row.Amount);
							}
						}
						else {
							for (var i in value) {
								count += parseFloat((typeof (value[i].Amount) == 'undefined' || value[i].Amount == "") ? '0' : value[i].Amount);
							}
						}
						var total = parseFloat((typeof (count) == 'undefined' || count == "") ? '0' : count).toFixed(2);

						if ('@Model.AuditedStatus' == 'False' && @Model.Id==0) {
                            $("#hidTotal").val(total);
							$("#OweCash").val("0.00");

                            var accounts = $("#FinancialIncomeItems_Partial_Form").find("input[name='CollectionAmount']");
                            $.each(accounts, function (i, input) {
                                $(input).val(total);
                            });
                        }

						return total;
					}
				},
				{
					field: 'Remark',
                    title: '备注',
                    formatter: $(this).formatIsNull
				},
				{
					field: 'Operate',
					title: '操作',
					align: 'center',
					width: '100',
					events: {
						'click .rowAppend': function (e, value, row, index) {// 添加
							//审核、红冲后不能修改
							if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
								return false;
							}
							$('#FinancialIncomeItems').bootstrapTable('insert', row);
						},
						'click .rowDel': function (e, value, row, index) {// 删除
							//审核、红冲后不能修改
							if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
								return false;
							}
							var datas = $('#FinancialIncomeItems').bootstrapTable('getData');
							var len = datas.length;
							if (len == 1) {
								$(this).showToastr('数据不能删除');
								return;
							}

							if (row.uniqueid == undefined) {
								$("#FinancialIncomeItems").bootstrapTable('remove', { field: "id", values: [parseInt(row.Id)] });
							}
							else {
								$("#FinancialIncomeItems").bootstrapTable('remove', { field: "uniqueid", values: [parseInt(row.uniqueid)] });
							}
						}
					},
					formatter: function (value, row, index) {
						return [
							'<button type="button" class="btn btn-sm btn-default rowAppend" title="添加"><i class="fa fa-plus" ></i></button>',
							'<button type="button" class="btn btn-sm btn-default rowDel" title="删除"><i class="fa fa-trash-o"></i></button>'
						].join('');
					}
				}
			],
			onClickCell: function (field, value, row, $element) {

				if (field == "Operate") {
					return false;
				}

				var input = $($element).find("input");
				var select = $($element).find("select");
				if (input.length > 0 || select.length > 0)
					return false;
				//当前列所在行的索引
				var index = $element.parent().attr("data-index");

				//审核、红冲后不能修改
				if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
					return false;
				}

				if (field == "AccountingOptionName") {
					var html = '<div class="input-group">' +
						'<input type="text" class="form-control input-sm"  name="AccountingOptionName" value="' + (typeof (value) == 'undefined' ? "" : value) + '" id="' + field + "_" + row.Id + '">' +
						'<span class="input-group-btn"><button type="button" class="btn btn-default btn-sm searchproduct" id="search_' + field + "_" + row.Id + '" data-index="' + index + '"><span class="glyphicon glyphicon-search"></span></button></span>' +
						'</div>';
					$($element).html(html);
					var inputObj = $("#search_" + field + "_" + row.Id);
					//选择科目
					inputObj.bind("click", function () {
                        var selectIndex = $(this).attr("data-index");
                        var PageTable = "FinancialIncomeItems";
						var actionUrl = "/Accounting/AsyncSearchSelectPopup?Multi=0&self=1&ifcheck=1&selectIndex=" + selectIndex + "&PageTable=" + PageTable + "&billTypeId=" + $("#BillTypeEnumId").val();

						$(this).accountingSingleSelectPopup($("#AccountingModalWindow"), actionUrl, 1, "选择科目", $("#AccountingForm"), selectIndex, function (options) {
							$("#FinancialIncomeItems").bootstrapTable('updateRow', {
								index: selectIndex, row: { AccountingOptionId: options[0].Id, AccountingOptionName: options[0].name }
							});
						});
						return false;
					});
				}
				else if (field == "Amount") {
					var html = '<input type="text" class="form-control input-sm" value="' + (typeof (value) == 'undefined' ? "0" : value) + '" id="' + field + "_" + row.Id + '">';
					$($element).html(html);
					var inputObj = $("#" + $(html).attr("id"));
					inputObj.focus();
					inputObj.select();
					inputObj.bind("focusout", function () {
						if (!isNaN($(this).val())) {
							row[field] = $(this).val();

							$("#FinancialIncomeItems").bootstrapTable('updateRow', { index: index, row: row });
							$($element).html("" + value + "");
						}
						else {
							$(this).showToastr("数字格式不正确!");
							return false;
						}


					});
				}
				else if (field == "Remark") {
					var html = '<input type="text" class="form-control input-sm" value="' + (typeof (value) == 'undefined' ? "" : value) + '" id="' + field + "_" + row.Id + '">';
					$($element).html(html);
					var inputObj = $("#" + $(html).attr("id"));
					inputObj.focus();
					inputObj.select();
					inputObj.bind("focusout", function () {
						$("#FinancialIncomeItems").bootstrapTable('updateRow', { index: index, row: { Remark: $(this).val() } });
						$($element).html("" + value + "");
					});
				}
				//debug
				$.each($('#FinancialIncomeItems').bootstrapTable('getData'), function (i, row) {
					//console.log(row);
				});
			}
		});

        //获取预付金额
        function SumAdvancePaymentAmount() {
            var sumAdvancePaymentAmount = 0;
            var colls = $("input[name='CollectionAmount']");
            for (var i = 0; i < colls.length; i++) {
                if ($(colls[i]).attr("data-codetype") == 14) {
                    if (isNaN($(colls[i]).val()) || $(colls[i]).val() == "") {
                        $(colls[i]).val("0.00")
                    }
                    sumAdvancePaymentAmount = parseFloat(sumAdvancePaymentAmount) + parseFloat(colls[i].value);
                }
            }
            return sumAdvancePaymentAmount;
		}

        //获取预收金额
        function SumAdvanceReceiptAmount() {
            var sumAdvanceReceiptAmount = 0;
            var colls = $("input[name='CollectionAmount']");
            for (var i = 0; i < colls.length; i++) {
                if ($(colls[i]).attr("data-codetype") == 24) {
                    if (isNaN($(colls[i]).val()) || $(colls[i]).val() == "") {
                        $(colls[i]).val("0.00")
                    }
                    sumAdvanceReceiptAmount = parseFloat(sumAdvanceReceiptAmount) + parseFloat(colls[i].value);
                }
            }
            return sumAdvanceReceiptAmount;
		}

        //计算已收金额
        function SumAlreadyAmount() {
            var sumAlreadyAmount = 0;
            var colls = $("input[name='CollectionAmount']");
            for (var i = 0; i < colls.length; i++) {

                if (isNaN($(colls[i]).val()) || $(colls[i]).val() == "") {
                    $(colls[i]).val("0.00")
                }

                sumAlreadyAmount = parseFloat(sumAlreadyAmount) + parseFloat(colls[i].value);
            }
            return sumAlreadyAmount;
        }

        //表单检查
		var formValidator = function () {
            var isVaild = true;

            var allTableData = $('#FinancialIncomeItems').bootstrapTable('getData');
            var accounts = $("#FinancialIncomeItems_Partial_Form").find("input[name='CollectionAmount']");
            var flag = 0;
            var numFlag = 0;
            var accFlag = 0;
            $.each(allTableData, function (i, row) {
                if (row.AccountingOptionId == "" || row.CustomerOrManufacturerId == "" || row.Amount == "" || row.Amount == "0")
                    flag++;
                else if (isNaN(row.Amount))
                    numFlag++;
            });

            $.each(accounts, function (i, input) {
                if (isNaN($(input).val()))
                    accFlag++;
            });

			if ($("#SalesmanId").val() == "" || $("#SalesmanId").val() == "0") {
				$("#SalesmanId").focus();
				$(this).showToastr("请先选择业务员！");
				return false;
			}
            else if (($("#TerminalId").val() == "" || $("#TerminalId").val() == "0") && ($("#ManufacturerId").val() == "" || $("#ManufacturerId").val() == "0")) {
                $(this).showToastr("请选择客户或供应商!");
                return false;
			}
			else if (flag == allTableData.length) {
				$(this).showToastr("请确认项目完整！");
				return false;
			}
			else if (numFlag > 0) {
				$(this).showToastr("项目单据中数字格式不正确!");
				return false;
			}
			else if (accFlag > 0) {
				$(this).showToastr("收款账户信息中数据格式不正确!");
				return false;
			}

            return isVaild;
        }

		//保存服务器
        var submitRemoteServer = function (showMessage, doAudit, callback,loadingButton) {
			if (!formValidator()) {
	            if(loadingButton != undefined){
				   loadingButton.stop();
			    }
				return false;
			};

			//获取表格的所有内容行
			var allTableData = $('#FinancialIncomeItems').bootstrapTable('getData');
			var serializeData = [];
			var accountings = [];
			$.each(allTableData, function (i, row) {
                var tmpObj =
                {
					Id: typeof (row.Id) == 'undefined' ? "0" : row.Id,
					AccountingOptionId: typeof (row.AccountingOptionId) == 'undefined' || row.AccountingOptionId == "" ? "0" : row.AccountingOptionId,//费用类别
	                CustomerOrManufacturerType: typeof (row.CustomerOrManufacturerType) == 'undefined' || row.CustomerOrManufacturerType == "" ? "0" : row.CustomerOrManufacturerType,//客户类型
					CustomerOrManufacturerId: typeof (row.CustomerOrManufacturerId) == 'undefined' || row.CustomerOrManufacturerId == "" ? "0" : row.CustomerOrManufacturerId,//客户
					Amount: typeof (row.Amount) == 'undefined' || row.Amount == "" ? "0" : row.Amount,//金额
					Remark: typeof (row.Remark) == 'undefined' ? "" : row.Remark,//备注
				};
				serializeData.push(tmpObj);
			});


			//获取收款账户
			var accounts = $("#FinancialIncomeItems_Partial_Form").find("input[name='CollectionAmount']");
			$.each(accounts, function (i, input) {
				var option = $(input).attr("data-account");
				var amount = $(input).val();
				accountings.push({ AccountingOptionId: option, CollectionAmount: $(input).val() });
			});

			//保存的数据
			var postData = {
				//员工
                SalesmanId: $("#SalesmanId").val(),
                TerminalId: $("#TerminalId").val(),
				ManufacturerId: $("#ManufacturerId").val(),
				//备注
				Remark: $("#Remark").val(),
				//欠款
                OweCash: $("#OweCash").val(),
				//单据项目
				Items: serializeData,
				//收款账户
				Accounting: accountings,
				//预收金额
                AdvanceAmount: SumAdvanceReceiptAmount(),
				//预付金额
				AdvancedPaymentsAmount: SumAdvancePaymentAmount()
			};

			//debug
			//console.log(postData);
			//return false;

            //保存服务器
            $(this).remoteSubmit("post", "/FinancialIncome/CreateOrUpdate?billId=@(Model.Id)&doAudit=" + doAudit, postData, $("#FinancialIncomeItems"), callback, showMessage,loadingButton);

		};

		//保存单据项目
		$("#FormSubmit").click(function () {
			var doAudit = true;
	        //防止重复保存
			var loadingButton = Ladda.create(this);
			loadingButton.start();
            submitRemoteServer(null, doAudit,function (data) {
                if (data.Success) {
                    window.location.href = "/FinancialIncome/List";
                }
            }, loadingButton);
		});

		//保存并继续
		$("#FormSubmitContinue").click(function () {
			var doAudit = false;
	        //防止重复保存
			var loadingButton = Ladda.create(this);
			loadingButton.start();
            submitRemoteServer(null, doAudit, function () { window.location.href = "/FinancialIncome/Create"; },loadingButton);
		});


		//审核
		$(document).on('click', "#btn_auditing", function () {
	        //防止重复保存
	        var loadingButton = Ladda.create(this);
			loadingButton.start();
			var returnUrl = "@Context.Request.Query["returnURL"]";
            if (returnUrl.indexOf("AuditedStatus=") == -1) {
                if (returnUrl.indexOf("?") == -1) {
                    returnUrl += "?";
                } else {
                    returnUrl += "&";
                }
                returnUrl += "AuditedStatus=False";
            } else {
                if (returnUrl.indexOf("AuditedStatus=False") == -1) {
                    returnUrl = returnUrl.replace("AuditedStatus=", "AuditedStatus=False");
                }
            }
            returnUrl = returnUrl.replace(/amp;/g, "");
            $(this).remoteSubmit("post", "/FinancialIncome/Auditing?id=@(Model.Id)", null, $("#FinancialIncomeItems"), function () {
                window.location.href = returnUrl;
				}, null,loadingButton);
		});

		//红冲
		$(document).on('click', "#btn_red", function () {
	        //防止重复保存
			var loadingButton = Ladda.create(this);

            WinMsg.confirm({ message: "确认要红冲吗？" }).on(function (e) {
				if (!e) {
					return;
				}
	            loadingButton.start();
				$(this).remoteSubmit("get", "/FinancialIncome/Reverse", { id: "@Model.Id" }, null, function () { location.href = "/FinancialIncome/List"; }, null,loadingButton);
			});
		});

		//打印
		$("#btn_print").click(function () {
			//防止重复保存
			var loadingButton = Ladda.create(this);
            WinMsg.confirm({ message: "确认要打印吗？" }).on(function (e) {
				if (!e) {
					return;
				}
				loadingButton.start();
                var setting = {}
                var LODOP = null;
                var rs = $(this).getSyncData("/ExchangeBill/PrintSetting", null, "get");
				if (rs.Success) {
					setting = rs.Data;
					loadCLodop(document, setting.PrintPort, () => {
						//判断页面是否已加载LODOP
						if (LODOP == null || LODOP == undefined) {
							LODOP = getLodopFirst(document);
							LODOP = getLodopSecond(document);
						}
						if (LODOP == null || LODOP == undefined) {
							loadingButton.stop();
							return;
						} else {
							$("#printData").html("");
						}
						$(this).remoteSubmit("get", "/FinancialIncome/Print", { selectData: "@Model.Id" }, null, function (result) {
							if (result.Success) {
                                var html = result.Data;
                                LODOP.PRINT_INIT("财务收入单");
                                if (!isNaN(setting.PaperWidth) && !isNaN(setting.PaperHeight)) {
                                    LODOP.SET_PRINT_PAGESIZE(1, setting.PaperWidth + "mm", setting.PaperHeight + "mm");
                                }
                                if (setting.IsPrintPageNumber) {
                                    LODOP.ADD_PRINT_HTM(1, 600, 300, 100, "总页号：<font color='#0000ff' format='ChineseNum'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>");
                                }
                                if (!isNaN(setting.MarginTop) && !isNaN(setting.MarginLeft) && !isNaN(setting.MarginRight) && !isNaN(setting.MarginBottom)) {
                                    LODOP.ADD_PRINT_HTM(setting.MarginTop + "mm", setting.MarginLeft + "mm", "RightMargin:" + setting.MarginRight + "mm", "BottomMargin:" + setting.MarginBottom + "mm", html);
								}
                                LODOP.SET_PRINT_STYLEA(0, "Horient", 2);
                                LODOP.PREVIEW(true);
                                loadingButton.stop();
							}
							else {
								$(this).showToastr(result.Message);
							}
						}, null, loadingButton);
					});
				}

			});
		});


        //选择终端
        $(document).on('click', "#btn_search_TerminalId", function () {
            var actionUrl = "/Terminal/AsyncSearchSelectPopup";
            $(this).terminalSearchSelectPopup($("#TerminalSelectModalWindow"), actionUrl, { Index: 0 }, "选择终端", $("#TerminalSelectForm"), null, function (data) {
            });
        });


        //选择供应商
        $(document).on('click', "#btn_search_ManufacturerId", function () {
            var actionUrl = "/Manufacturer/AsyncSearchSelectPopup";
            $(this).manufacturerSearchSelectPopup($("#ManufacturerSelectModalWindow"), actionUrl, { Index: 0 }, "选择供应商", $("#ManufacturerSelectForm"), null, function (data) {
            });
        });

		//科目
        $(document).on('change', "#FinancialIncomeItems_Partial_Form input[name='CollectionAmount']", function ()
        {
            $(this).focus();

            if (isNaN($(this).val())) {
                $(this).showToastr("只能输入数字!");
                $(this).val('0.0');
                return false;
            }

            if (parseFloat($(this).val()) < 0) {
                $(this).showToastr("只能输入正数!");
                $(this).val('0.0');
                return false;
			}

            var total = $("#hidTotal").val();

            var thisOwnCash = (parseFloat(total) - SumAlreadyAmount()).toFixed(2);
            //剩余欠款金额
            if (thisOwnCash < 0) {
                $(this).val("0.00");
                $("#OweCash").val(parseFloat(parseFloat(total) - SumAlreadyAmount()).toFixed(2));
            } else {
                $("#OweCash").val(thisOwnCash);
            }
		});

        //客户
        window.operateEventsPopTerminal = {
            'click .rowSelect': function (e, value, row, index) {
                try
                {
                    $("#TerminalId").val(row.Id);
					$("#TerminalName").val(row.Name);
                    $("#ManufacturerId").val("");
                    $("#ManufacturerName").val("");
                    $('#TerminalSelectModalWindow').modal('hide');
                }
                catch (err) {
                    console.log(err);
                    $(this).showToastr(err);
                    return;
                }
            }
        };

		//供应商
		window.operateEventsPopManufacturer = {
			'click .rowSelect': function (e, value, row, index) {
                try
                {
                    $("#ManufacturerId").val(row.Id);
					$("#ManufacturerName").val(row.Name);
                    $("#TerminalId").val("");
                    $("#TerminalName").val("");
                    $('#ManufacturerSelectModalWindow').modal('hide');
				}
				catch (err) {
					console.log(err);
					$(this).showToastr(err);
					return;
				}
			}
		};

	});
</script>